#coding:gbk
import xlrd
from os.path import *
import os
import fnmatch
import shutil
import win32api
import glob


curDir = './'
workDir = './testarea'
workDir = os.path.abspath(workDir)
ansfile_kDir = './ansfile_k'

def getrow(filename='windows_1.xls'):
    'get a row from an excel file'
    
    bk = xlrd.open_workbook(join(curDir,ansfile_kDir,filename))
    bksheet1 = bk.sheets()[0]
    for n in range(bksheet1.nrows):
        yield bksheet1.row(n)

class robot4win(object):
    def __init__(self,mydir = workDir):
        self.mydir = mydir
        
    def ISFLDREXIST(self,directory):
        'the command check a directory'
        try:
            os.mkdir(join(self.mydir,directory))
        except:
            print 'catch the same dir',join(self.mydir,directory)
        
        
    def ISFLDRDEL(self,floder):
        'check if the floder is deleted'
        os.rmdir(join(self.mydir,floder))
        
    def ISFLDRCOPY(self,src,dest):
        'check if floder is copied'
        shutil.copytree(join(self.mydir,src), join(self.mydir,dest))

    def ISFILEMRENAME(self,src,dest):
        'check if file moved to destnation'
        os.rename(join(self.mydir,src), join(self.mydir,dest))

    def ISFILECOPY(self,sourceDir,destnation,wildcard):
        'this commmand check if files can copied to the destnation'
        for root, dirs, files in os.walk(join(self.mydir,sourceDir)):
            if abspath(root) == abspath(join(self.mydir,destnation)):
                continue
            else:
                for myfile in files:
                    if fnmatch.fnmatch(join(root,myfile),wildcard):
                        shutil.copyfile(join(root,myfile),join(self.mydir,destnation,myfile))
    def ISFILEMOVE(self, srcDir,dest,wildcard):
        'move a dicectory file filterd by wildcard to destnation'
        for myfile in glob.glob(join(self.mydir,srcDir,wildcard)):
            os.rename(join(self.mydir,srcDir,myfile),
                      join(self.mydir,dest,myfile))
    
        

    def FILESX(self,filepath,attr):
        'check file attr'
        path = join(self.mydir,filepath).encode('gbk')
        win32api.SetFileAttributes(path,int(attr))
    def ISKJFSEXIST(self,path,afile):
        'this command actually only check file exist'
        open(join(self.mydir,path,afile),'wb').close()

    
    def END(self):
        'do nothing, meet the end'
        pass
    
def do_it():
    robot = robot4win(workDir)
    for row in getrow():
        commandline = [cell.value for cell in row][:-2]
        #get command and argment count
        command = getattr(robot, commandline[0])
        argcount = command.func_code.co_argcount
        ##print command,argcount
        #rock and roll
        command(*commandline[1:argcount])
        
                    
                
        
        

if __name__ == '__main__':
    
    do_it()
##    for row in getrow():
##        for cell in row:
##            print cell.value,' ',
##    robot = robot4win(workDir)
##    robot.ISFLDREXIST('winkt112')
    
